package com.uton.rfidcontroller.customize;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.interfaces.IPublicController;
import com.uton.rfidcontroller.common.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.view.KeyEvent.KEYCODE_ENTER;

/**
 * Created by 王家宝 on 2017/9/1.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class SearchView extends RelativeLayout {
    @BindView(R.id.et_search_contract)
    EditText etSearch;
    @BindView(R.id.iv_search_contract)
    ImageView ivSearch;
    @BindView(R.id.tv_search_contract)
    TextView tvSearch;
    @BindView(R.id.iv_clear_contract)
    ImageView ivClear;
    @BindView(R.id.tv_cancel_contract)
    TextView tvCancel;
    @BindView(R.id.rl_search_contract)
    RelativeLayout rlSearchContract;

    private Context context;

    private IPublicController.ISearchController mSearchController;

    public SearchView(Context context) {
        super(context);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.customize_searchview, this);
        ButterKnife.bind(this);
        initSearchSystem(context);
    }

    private void initSearchSystem(final Context context) {
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().length() > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                } else {
                    ivClear.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEND || (keyEvent != null
                        && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    mSearchController.doOnSearch(etSearch.getText().toString());
                    Utils.closeKeybord(etSearch, context);
                    return false;
                }
                return false;
            }
        });

        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etSearch.setText("");
                ivClear.setVisibility(View.GONE);
                etSearch.setFocusable(true);
                etSearch.setFocusableInTouchMode(true);
                etSearch.requestFocus();
                Utils.openKeybord(etSearch, context);
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etSearch.setText("");
                etSearch.setVisibility(View.GONE);
                tvSearch.setVisibility(View.VISIBLE);
                tvCancel.setVisibility(View.GONE);
                ivSearch.setVisibility(View.VISIBLE);
                mSearchController.doOnCancel(etSearch.getText().toString());
                Utils.closeKeybord(etSearch, context);
            }
        });


        rlSearchContract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ivSearch.setVisibility(View.GONE);
                tvSearch.setVisibility(View.GONE);
                etSearch.setVisibility(View.VISIBLE);
                tvCancel.setVisibility(View.VISIBLE);
                etSearch.setFocusable(true);
                etSearch.setFocusableInTouchMode(true);
                etSearch.requestFocus();
                Utils.openKeybord(etSearch, context);
            }
        });

    }

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setSearchController(IPublicController.ISearchController searchController) {
        mSearchController = searchController;
    }


    public void closeSearch() {
        Utils.closeKeybord(etSearch, context);
        etSearch.setText("");
        mSearchController.doOnCancel(etSearch.getText().toString());
        etSearch.setVisibility(View.GONE);
        tvSearch.setVisibility(View.VISIBLE);
        tvCancel.setVisibility(View.GONE);
        ivClear.setVisibility(View.GONE);
        ivSearch.setVisibility(View.VISIBLE);
    }

}
